package eva.savant.core.service;

import javax.persistence.EntityManager
import javax.persistence.PersistenceContext
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional
import org.springframework.dao.DataAccessException

@Repository(value = 'entityService')
public class EntityServiceImpl implements EntityService {


  @PersistenceContext EntityManager em = null


  public Object getEntity(String queryService, Object... values) {

    return getEntity(queryService, values as List)
  }

  public Object getEntity(String queryService, List values) {

    return findSingleResultNamedQuery(queryService, values)
  }

  public List getLsEntity(String queryService, Object... values) {

    return getLsEntity(queryService, values as List)
  }

  @Transactional(readOnly = true)
  public List getLsEntity(String queryService, List values) {

    if(queryService == null || values == null) return null

    def query = em.createNamedQuery(queryService)

    for (int i= 0; i < values.size(); i++)

    query.setParameter(i+1,values[i])

    return query.resultList
  }


  @Transactional(readOnly = true)
  public <T> List<T> find(Class<T> tClass, String queryString, Object... values) {

    def query = em.createQuery(queryString,tClass)

    for (int i= 0; i < values.size(); i++)

    query.setParameter(i+1,values[i])

    return query.resultList
  }

    @Transactional(readOnly = true)
    public List find(String queryString, Object... values) {

        def query = em.createQuery(queryString)

        for (int i= 0; i < values.size(); i++)

            query.setParameter(i+1,values[i])

        return query.resultList
    }


    @Transactional(readOnly = true)
  public List find(final String queryString, final List values) {

    def query = em.createQuery(queryString)

    for (int i= 0; i < values.size(); i++)

    query.setParameter(i+1,values[i])

    return query.resultList
  }

  @Transactional(readOnly = true)
  public <T> T find(Class <T> entityClass, Object primaryKey) throws DataAccessException {

    return em.find(entityClass, primaryKey)
  }

  @Transactional
  public void persist(Object entity) {

    em.persist(entity)
  }

  @Transactional
  public <T> T merge(T entity) {

    return em.merge(entity)
  }

  @Transactional
  public <T> T remove(T entity) {

    return em.merge(entity)
  }

  @Transactional
  public void delete(Object entity) {

    em.remove(entity)
  }

  public <T> T getReference(Class<T> tClass, Object primaryKey) {

    em.getReference(tClass, primaryKey)
  }

  public void flush() {

    em.flush()
  }

  public Object findSingleResult(String queryString, List values) {

    def model= find(queryString, values)

    return model != null && model.size() > 0? model[0] : null
  }

  public Object findSingleResultNamedQuery(final String name, final List values) {

    def model= getLsEntity(name, values)

    return model != null && model.size() > 0? model[0] : null
  }

}
